<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
	case "modify" :
		// validate from / to dates
		$FromDate = validate_date($_GET['DateFrom'],1);
		if (! $FromDate) $message .= "<br>" . $langinvaliddate . " : " . $langdatefrom . "<br>";
		$ToDate = validate_date($_GET['DateTo'], 1);
		if (! $ToDate) $message .= "<br>" . $langinvaliddate . " : " . $langdateto . "<br>";
		
		// To Date should be greater than or equal to From Date
		if ($FromDate > $ToDate) $message .= "<br>" . $langinvaliddaterange . "<br>";
		
		// validate from time
		$from_time_error = 0;
		if (! validate_number($_GET['FromHour'],0) || $_GET['FromHour'] > 24 || $_GET['FromHour'] < 0)
			$from_time_error = 1;
		if (! validate_number($_GET['FromMinute'],0) || $_GET['FromMinute'] > 59 || $_GET['FromMinute'] < 0) 
			$from_time_error = 1;
		if ($from_time_error) $message .= "<br>" . $langinvalidtime . " : " . $langfrom . "<br>";
		
		// validate to time
		$to_time_error = 0;
		if (! validate_number($_GET['ToHour'],0) || $_GET['ToHour'] > 24 || $_GET['ToHour'] < 0) 
			$to_time_error = 1;
		if (! validate_number($_GET['ToMinute'],0) || $_GET['ToMinute'] > 59 || $_GET['ToMinute'] < 0) 
			$to_time_error = 1;
		if ($to_time_error) $message .= "<br>" . $langinvalidtime . " : " . $langto . "<br>";
		
		// validate time slice
		$time_slice_err = 0;
		if (! validate_number($_GET['TimeSlice'],0) || $_GET['TimeSlice'] > 60 || $_GET['TimeSlice'] < 0) {
			$message .= "<br>" . $langinvalidtimeslice . "<br>";
			$time_slice_err = 1;
		}
		
		// validate combination of from time / to time / time slice
		if (! $message) {
			// from time must be smaller than to time
			if ($_GET['FromHour'] > $_GET['ToHour']) $message .= "<br>" . $langinvalidtimerange . "<br>";
			if ($_GET['FromHour'] == $_GET['ToHour'] && $_GET['FromMinute'] >= $_GET['ToMinute']) 
				$message .= "<br>" . $langinvalidtimerange . "<br>";
			
			// time slice should be more than 1
			$total_minutes = ($_GET['ToHour'] * 60 + $_GET['ToMinute']) - 
							 ($_GET['FromHour'] * 60 + $_GET['FromMinute']);
			$no_of_slice = $total_minutes / $_GET['TimeSlice'];
			if ($no_of_slice < 1) $message .= "<br>" . $langtimerangetoosmall . "<br>";
			
			// Should not have fractional time slice within time range
			if (! validate_number($no_of_slice, 0)) $message .= "<br>" . $langfractionaltimeslice . "<br>";
		}
		
		// check the change of time slice does not affect existing unbill transactions
		if (! $message) {
			if ($_GET['TimeSlice'] != $_SESSION['TimeSlice']) {
				$sql = "SELECT FromTime, ToTime FROM billtransaction WHERE billno is null AND FromTime is not null";
				$sql_result = sql_execute($sql);
				$bill_time_slice_err = 0;
				while ($sql_data = mysql_fetch_array($sql_result)) {
					$begin_slot = get_time_slot_from_time($sql_data['FromTime'], $_GET['TimeSlice']);
					$end_slot = get_time_slot_from_time($sql_data['ToTime'], $_GET['TimeSlice']);
					if (! validate_number($begin_slot,0) || ! validate_number($end_slot,0)) {
						$message .= "<br>" . $langtimesheetsloterr . "<br>";
						break;	
					}
				}
			}
		}
			
		if ($message) {
			message_box($message);
			?>
			<script>history.go(-1)</script>
			<?
			exit;
		}
		
		$sql = "UPDATE openperiod SET " .
			   "FromDate = '$FromDate', " . 
			   "ToDate = '$ToDate', " .
			   "FromHour = " . $_GET['FromHour'] . ", " .
			   "FromMinute = " . $_GET['FromMinute'] . ", " .
			   "ToHour = " . $_GET['ToHour'] . ", " .
			   "ToMinute = " . $_GET['ToMinute'] . ", " .
			   "TimeSlice = " . $_GET['TimeSlice'] ;
		$sql_result = sql_execute($sql);
		
		$_SESSION['FromDate'] = $FromDate;
		$_SESSION['ToDate'] = $ToDate;
		$_SESSION['FromHour'] = $_GET['FromHour'];
		$_SESSION['ToHour'] = $_GET['ToHour'];
		$_SESSION['FromMinute'] = $_GET['ToMinute'];
		$_SESSION['ToMinute'] = $_GET['ToMinute'];
		$_SESSION['TimeSlice'] = $_GET['TimeSlice'];
		
		echo "<br>$langopenperiod $langmodified<br>";
		break;
		
	default :

		$DateFrom = convert_mysql_date($_SESSION['FromDate']);
		$DateTo = convert_mysql_date($_SESSION['ToDate']);
		?>
		<form method = "GET" action="openperiod.php">
		<table>
			<tr bgcolor='<? echo $color1 ?>'>
				<td><? echo $langopenperiodcontrol ?>
				<td><? echo $langfrom ?>
				<td><? echo $langto ?>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langdatestyle . " : " . $_SESSION['DateStyle'] ?>
				<td><input type = 'text' name = 'DateFrom' value = '<? echo $DateFrom ?>' 
					maxlength = '8'>
				<td><input type = 'text' name = 'DateTo' value= '<? echo $DateTo ?>'
					maxlength = '8'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langtime . " " . $langtimestyle ?>

				<td><input type = 'text' name = 'FromHour' value = '<? printf('%02d',$_SESSION['FromHour']) ?>' 
					maxlength = '2' size='2'> : 
					<input type='text' name = 'FromMinute' value = '<? printf('%02d',$_SESSION['FromMinute']) ?>' 
					maxlength = '2' size='2'>
				<td><input type = 'text' name = 'ToHour' value = '<? printf('%02d',$_SESSION['ToHour']) ?>'
					maxlength = '2' size='2'> :
					<input type = 'text' name = 'ToMinute' value = '<? printf('%02d',$_SESSION['ToMinute']) ?>' 
					maxlength = '2' size='2'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langtimeslice ?>
				<td><input type = 'text' name = 'TimeSlice' value = '<? printf('%02d',$_SESSION['TimeSlice']) ?>'
					maxlength = '2' size='2'>
				<td><input type = 'hidden' name = 'action' value = 'modify'>
		</table>
		<input type = 'submit' value = '<? echo $langmodify ?>'>		
		</form>
		<?	
		break;		
}

html_footer();
?>